package com.kye.express.business.jd.address;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.IView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.jd.address.JDAddressBean;
import com.kye.model.bean.jd.address.request.RequestDeleteAddress;
import com.kye.model.bean.jd.address.request.RequestJDAddress;
import com.kye.model.bean.user.User;
import com.kye.model.datasource.JDShopDataSource;

import java.util.List;

import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 31/01/2018
 * @updated : 31/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class JDAddressListConstract {

    interface JDAddressListView extends IView {

        void onUser(User user);

        void onLoading();

        void onLoaded();

        void refreshInitSuccess(List<JDAddressBean> jdAddressBeans);

        void refreshInitServerFailed(String msg);

        void refreshInitNetFailed(String message);

        void deleteAddressSuccess(OpenApiResult<Object> result, String s);

        void deleteAddressServerFailed(String msg);

        void deleteAddressNetFailed(String message);
    }

    public static class JDAddressListPresenter extends BasePresenter<JDAddressListConstract.JDAddressListView> {

        private JDShopDataSource jdShopDataSource;

        public JDAddressListPresenter() {
            jdShopDataSource = new JDShopDataSource();
        }

        @Override
        public void bind(JDAddressListView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Exception {
                            getView().onUser(user);
                        }
                    });
        }

        public void refreshInit(User userInfo, JDAddressBean currentSelectedAddress) {
            Single.just(new RequestJDAddress())
                    .doOnSuccess(new Consumer<RequestJDAddress>() {
                        @Override
                        public void accept(RequestJDAddress requestJDAddress) throws Exception {
                            requestJDAddress.setPhone(userInfo.getPhone());
                            requestJDAddress.setAddressType("0");
                        }
                    })
                    .flatMap(new Function<RequestJDAddress, SingleSource<OpenApiResult<List<JDAddressBean>>>>() {
                        @Override
                        public SingleSource<OpenApiResult<List<JDAddressBean>>> apply(RequestJDAddress requestJDAddress) throws Exception {
                            return jdShopDataSource.getJDShowAddress(requestJDAddress)
                                    .compose(getView().bindToLifecycleDestroy());
                        }
                    })
                    // 初始化选中
                    .doOnSuccess(new Consumer<OpenApiResult<List<JDAddressBean>>>() {
                        @Override
                        public void accept(OpenApiResult<List<JDAddressBean>> listOpenApiResult) throws Exception {
                            if (currentSelectedAddress == null) {
                                return;
                            }
                            if (listOpenApiResult.isSuccess()) {
                                List<JDAddressBean> jdAddressBeans = listOpenApiResult.getData();
                                for (JDAddressBean addressBean : jdAddressBeans) {
                                    if (addressBean.getId().equals(currentSelectedAddress.getId())) {
                                        addressBean.setSelected(true);
                                        break;
                                    }
                                }
                            }
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<List<JDAddressBean>>() {
                        @Override
                        public void onLoading(Disposable d) {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<List<JDAddressBean>> result, List<JDAddressBean> jdAddressBeans) {
                            getView().refreshInitSuccess(jdAddressBeans);
                        }

                        @Override
                        public void onApiError(OpenApiResult<List<JDAddressBean>> result, int code, String msg) {
                            getView().refreshInitServerFailed(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().refreshInitNetFailed(e.getMessage());
                        }

                        @Override
                        public void onLoaded() {

                        }
                    });
        }

        public void deleteAddress(User userInfo, JDAddressBean jdAddressBean) {
            Single.just(new RequestDeleteAddress())
                    .doOnSuccess(new Consumer<RequestDeleteAddress>() {
                        @Override
                        public void accept(RequestDeleteAddress requestDeleteAddress) throws Exception {
                            requestDeleteAddress.setAddress("0");
                            requestDeleteAddress.setId(jdAddressBean.getId());
                            requestDeleteAddress.setPhone(userInfo.getPhone());
                        }
                    })
                    .flatMap(new Function<RequestDeleteAddress, SingleSource<OpenApiResult<Object>>>() {
                        @Override
                        public SingleSource<OpenApiResult<Object>> apply(RequestDeleteAddress requestDeleteAddress) throws Exception {
                            return jdShopDataSource.deleteAddress(requestDeleteAddress)
                                    .compose(getView().bindToLifecycleDestroy());
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<Object>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<Object> result, Object o) {
                            getView().deleteAddressSuccess(result, result.getMsg());
                        }

                        @Override
                        public void onApiError(OpenApiResult<Object> result, int code, String msg) {
                            getView().deleteAddressServerFailed(result.getMsg());
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().deleteAddressNetFailed(e.getMessage());
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }
                    });
        }
    }
}
